package com.example.myapplication.ui;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import androidx.annotation.Nullable;

import com.example.myapplication.ImgInfo.PunishInfo;
import com.example.myapplication.R;
import com.example.myapplication.utils.HttpUtils;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class BarTextActivity extends Activity {
    private BarChart barChart;
    private Map<String, Integer> nv = new HashMap<>();
    private Map<String, Integer> nan = new HashMap<>();
    private Button btnn;
    private int index = 0;
    private int indexx = 0;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 200:
                    ShowBar();
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bartext);


        barChart = findViewById(R.id.my_bar2_chart);
        btnn = findViewById(R.id.btnn);


        btnn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        HttpUtils.Get("http://192.168.100.147:8080/skill/system/punishments", new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.i("aaa", "下载出错: ");
                e.printStackTrace();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String data = response.body().string();
                List<PunishInfo.Pun> puns = new Gson().fromJson(data, new TypeToken<List<PunishInfo.Pun>>() {
                }.getType());
                for (PunishInfo.Pun k : puns) {
                    if (k.getSex().equals("男")) {
                        if (nan.containsKey(k.getCity())) {
                            int num = nan.get(k.getCity());
                            num++;
                            nan.put(k.getCity(), num);
                        } else {
                            nan.put(k.getCity(), 1);
                        }
                    } else {
                        if (nv.containsKey(k.getCity())) {
                            int num = nv.get(k.getCity());
                            num++;
                            nv.put(k.getCity(), num);
                        } else {
                            nv.put(k.getCity(), 1);
                        }
                    }
                }
                handler.sendEmptyMessage(200);
            }
        });
    }

    private void ShowBar() {

        List<BarEntry> list = new ArrayList<>();
        final List<String> citys = new ArrayList<String>();
        for (String k : nan.keySet()) {
            list.add(new BarEntry( ++index,nan.get(k)));
            citys.add(k);
        }
        List<BarEntry> listts = new ArrayList<>();
        for (String k : nv.keySet()) {
            listts.add(new BarEntry(++indexx,nv.get(k)));
        }
        BarDataSet barDataSet = new BarDataSet(list, "男");
        BarDataSet barDataSet2 = new BarDataSet(listts, "女");
        //设置颜色
        barDataSet.setColor(Color.RED);
        barDataSet2.setColor(Color.GREEN);
        //多重数据集
        List<IBarDataSet> list1 = new ArrayList<>();
        list1.add(barDataSet);
        list1.add(barDataSet2);
        //data填充dataset
        BarData data = new BarData(list1);
        //宽度
        data.setBarWidth(0.1f);

        //间距
        //barChart.setPadding(4,2,4,2);
        barChart.getXAxis().setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float v, AxisBase axisBase) {

                int n = (int)v;
//                Log.i("aaaaa",""+v);
                if(n>0 && n <=5){
                    return citys.get((n-1));
                }
                else{
                    return "";
                }
            }
        });
        //刻度设置
        barChart.getXAxis().setAxisMinimum(1);
        barChart.getXAxis().setAxisMaximum(list.size()+1);
        barChart.getXAxis().setGranularity(1);
        barChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
        barChart.getXAxis().setCenterAxisLabels(true);
        //填充数据及绘制
        barChart.setData(data);
        barChart.groupBars(1,0.6f,0.1f);

        barChart.invalidate();
    }
}
